layui.use(['table', 'form', 'jquery', 'common'], function() {
		let table = layui.table;
		let form = layui.form;
		let $ = layui.jquery;
		let common = layui.common;

		let cols = [
			[{
					type: 'checkbox'
				},
				{
					title: '系统订单号',
					field: 'trade_no',
					align: 'center',
					width: 100
				},
				{
					title: '商户订单号',
					field: 'out_trade_no',
					align: 'center'
				},
				{
					title: '商户号',
					field: 'pid',
					align: 'center',
				},
				{
					title: '回调域名',
					field: 'notify_url',
					align: 'center',
				},
				{
					title: '商品名称',
					field: 'name',
					align: 'center'
				},
				{
					title: '实际支付',
					field: 'price',
					align: 'center',
				},
				{
					title: '二维码ID',
					field: 'hook_type',
					templet: "#hook_type",
					align: 'center',
				},
				{
					title: '订单金额',
					field: 'money',
					align: 'center',
				},
				{
					title: '创建时间',
					field: 'addtime',
					align: 'center',
				},
				{
					title: '完成时间',
					field: 'endtime',
					templet: "#isPay",
					align: 'center',
				},
				{
					title: '支付方式(通道ID)',
					field: 'type',
					templet: "#type",
					align: 'center',
				},
				{
					title: '支付状态',
					field: 'status',
					templet: "#status",
					unresize: "true",
					align: 'center',
				},
				{
					title: '操作',
					toolbar: '#user-bar',
					align: 'center',
					width: 130
				}
			]
		]

		table.render({
			elem: '#user-table',
			url: '/h/ajax/ordergetDataList',
			page: true,
			cols: cols,
			cellMinWidth: 170,
			skin: 'line',
			toolbar: '#user-toolbar',
			defaultToolbar: [{
				title: '刷新',
				layEvent: 'refresh',
				icon: 'layui-icon-refresh',
			}, 'filter', 'print', 'exports']
		});

		table.on('tool(user-table)', function(obj) {
			if (obj.event === 'deleteOrder') {
				window.deleteOrder(obj);
			}else if (obj.event === 'allRemove') {
				window.allRemove(obj);
			}
		});

		table.on('toolbar(user-table)', function(obj) {
			if (obj.event === 'refresh') {
				window.refresh();
			} else if (obj.event === 'batchRemove') {
				window.batchRemove(obj);
			} else if (obj.event === 'allRemove') {
				window.allRemove(obj);
			}
		});

		form.on('submit(user-query)', function(data) {
			// console.log(data.field)
			table.reload('user-table', {
				where: data.field,
				page: {
					curr: 1
				}
			})
			return false;
		});

		form.on('switch(statu)', function(obj) {
			var trade_no = this.value;
			// var load = layer.load();
			$.ajax({
                url: "/h/ajax/orderstatus",
                type: "POST",
                dataType: 'json',
                data:{
                    trade_no:trade_no
                },
                success: function(result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 1000
                        }, function() {
                            table.reload('user-table');
                        });
                        
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                },
                error:function(){
                    layer.msg("服务器错误！", {
                        icon: 2,
                        time: 1000
                    });
                }
            })
		});

		window.deleteOrder = function(obj) {
			console.log(obj.data['trade_no'])
			layer.confirm('确定要删除该订单', {
				icon: 3,
				title: '提示'
			}, function(index) {
				layer.close(index);
				let loading = layer.load();
				$.ajax({
					url: "/h/ajax/orderdelete",
					type: "POST",
					data: {
						trade_no: obj.data['trade_no']
					},
					dataType: 'json',
					success: function(result) {
						layer.close(loading);
						if (result.code == 200) {
							layer.msg(result.msg, {
								icon: 1,
								time: 1000
							}, function() {
								obj.del();
							});
							table.reload('user-table');
						} else {
							layer.msg(result.msg, {
								icon: 2,
								time: 1000
							});
						}
					},
                    error:function(){
                        layer.close(loading);
                        layer.msg("服务器错误！", {
                            icon: 2,
                            time: 1000
                        });
                    }
				})
			});
		}


		window.allRemove = function(obj) {
			layer.confirm('确定要清理未支付订单', {
				icon: 3,
				title: '提示'
			}, function(index) {
				layer.close(index);
				let loading = layer.load();
				$.ajax({
					url: "/h/ajax/orderallRemove",
					type: "GET",
					dataType: 'json',
					success: function(result) {
						layer.close(loading);
						if (result.code == 200) {
							layer.msg(result.msg, {
								icon: 1,
								time: 1000
							}, function() {
								obj.del();
							});
							table.reload('user-table');
						} else {
							layer.msg(result.msg, {
								icon: 2,
								time: 1000
							});
						}
					},
                    error:function(){
                        layer.close(loading);
                        layer.msg("服务器错误！", {
                            icon: 2,
                            time: 1000
                        });
                    }
				})
			})
		}

		window.batchRemove = function(obj) {

			var checkIds = common.checkField(obj, 'trade_no');
			if (checkIds === "") {
				layer.msg("未选中数据", {
					icon: 3,
					time: 1000
				});
				return false;
			}

			layer.confirm('确定要删除这些订单', {
				icon: 3,
				title: '提示'
			}, function(index) {
				layer.close(index);
				let loading = layer.load();
				$.ajax({
					url: "/h/ajax/orderbatchRemove",
					data: {
						ids: checkIds
					},
					dataType: 'json',
					type: 'POST',
					success: function(result) {
						layer.close(loading);
						if (result.code == 200) {
							layer.msg(result.msg, {
								icon: 1,
								time: 1000
							}, function() {
								table.reload('user-table');
							});
						} else {
							layer.msg(result.msg, {
								icon: 2,
								time: 1000
							});
						}
					},
                    error:function(){
                        layer.close(loading);
                        layer.msg("服务器错误！", {
                            icon: 2,
                            time: 1000
                        });
                    }
				})
			});
		}

		window.refresh = function(param) {
			table.reload('user-table');
		}
	})